import store from './index'

function find(str, cha, num) {
  var x = str.indexOf(cha)
  for (var i = 0; i < num; i++) {
    x = str.indexOf(cha, x + 1)
  }
  return x
}
const mutations = {
  changeCollapse(state, param) {
    state.collapse = param
  },
  changeIsShow(state, param) {
    state.isShow = param
  },
  SET_KEEP_ALIVE: function (state, keekAlives) {
    if (state.keekAlives.indexOf(keekAlives) == '-1') {
      state.keekAlives.push(keekAlives)
      state.flag = false
    } else {
      if (state.flag) {
        // var index = state.keekAlives.indexOf(keekAlives);
        // if (index > -1) {
        //   state.keekAlives.splice(index, 1);
        // }
        for (var i = state.keekAlives.length - 1; i >= 0; i--) {
          if (state.keekAlives[i] == keekAlives) {
            state.keekAlives.splice(i, 1)
          }
        }
        state.flag = false
        setTimeout(function () {
          state.keekAlives.push(keekAlives)
        }, 1000)
      }
    }
  },
  /**
   * @description: 重要通知
   */
  setNoticeDate(state, param) {
    state.noticeDate = param
  },
  /**
   * @description: 鄂州项目id
   */
  setEZhouprojectId(state, param) {
    state.eZhouprojectId = param
  },
  /**
   * @description: 鄂州项目名字
   */
  setEZhouprojectName(state, param) {
    state.eZhouprojectName = param
  },
  /**
   * @description: 鄂州项目视频监控
   */
  setEZhouVideoShowSrc(state, param) {
    state.videoShowSrc = param
  },
  /**
   * @description: 鄂州项目视频监控
   */
  setIsShowVideo(state, param) {
    state.isShowVideo = param
  },
  /**
   * @description: 鄂州项目区域
   */
  setEZhouArea(state, param) {
    state.eZhouArea = param
  },
  setpageNo(state, param) {
    state.pageNo = param
  },
  ADD_SLIDER(state, value) {
    state.flag = true
    var data = state.sliderBar.filter((f) => {
      if (f.id == value.id) {
        delete f.newName
        return f
      }
    })
    if (data.length == 0) {
      delete value.newName
      //首页后面带参数
      // var i = find(value.url, "?", 0)
      // if(i != "-1"){
      //   var str = value.url.slice(0, i);
      // }

      if (value.url == '/home') {
        state.activeIndex = '/home'
      } else {
        state.sliderBar.push(value)
        // state.flag = true;
      }
    }
    localStorage.setItem('sliderBar', JSON.stringify(state.sliderBar))
  },
  DELETE_SLIDER(state, value) {
    // 点击删除清除缓存
    var valueName = find(value, '/', 2)
    var index = state.keekAlives.indexOf(value.slice(valueName + 1))
    if (index > -1) {
      state.keekAlives.splice(index, 1)
    }
    var indexTar = ''
    state.sliderBar.forEach((item, index) => {
      if (item.url == value) {
        indexTar = index
      }
    })
    var deleteItem = state.sliderBar.splice(indexTar, 1)
    if (
      deleteItem[0].url == state.activeIndex ||
      deleteItem[0].newName == state.activeIndex
    ) {
      state.activeIndex = state.sliderBar[indexTar - 1].url
      state.activeMenu = state.sliderBar[indexTar - 1].url
    }
    localStorage.setItem('sliderBar', JSON.stringify(state.sliderBar))
  },
  // 设置当前激活的tab
  set_active_index(state, index) {
    var index = decodeURI(index)
    if (
      state.sliderBar.filter((f) => f.url == index || f.newName == index)
        .length == 0
    ) {
      var m

      // state.menuList.forEach(menu => {
      state.menuListTotal.forEach((menu) => {
        if (menu.child) {
          menu.child.forEach((c) => {
            //如果跳转路由有参数就截取，没有参数就等于自己
            var str
            if (find(index, '?', 0) != -1) {
              var i = find(index, '?', 0)
              str = index.slice(0, i)
            } else {
              str = index
            }
            if (c.url && c.url.indexOf(str) != -1) {
              m = c
            }
          })
        }
      })
      if (m && state.sliderBar.filter((f) => f.url == m.url).length == 0) {
        m.newName = index
        state.sliderBar.push(m)
      }
    }
    state.activeIndex = index
    // console.log(state.activeIndex)
  },
  CHANGE_SLIDER_NAME(state, url) {
    url.newUrl = localStorage.getItem('toFullPath')
    state.flag = true
    // state.sliderBar.filter(f => {
    //   if (f && f.url == url.oldUrl) {
    //     f.newName = decodeURI(url.newUrl)
    //   }
    // })
    //增加标识，判断是否有已经选中的父节点，有就直接选中
    //若无，则看是否有可以添加的父节点
    var flag = ''
    state.sliderBar.filter((f) => {
      if (f && f.url == url.oldUrl) {
        flag = true
        f.newName = decodeURI(url.newUrl)
        state.activeMenu = url.oldUrl
      }
    })
    // 增加逻辑
    if (!flag) {
      //先看顶部选中的是否有，若有则在顶部选中的里面找，没有则全局找
      var arr = []
      for (var i = 0; i < state.menuList.length; i++) {
        var item = state.menuList[i]
        if (item.child) {
          for (var j = 0; j < item.child.length; j++) {
            arr.push(item.child[j])
          }
        }
      }
      var isSelected = ''
      arr.filter((f) => {
        if (f && f.url == url.oldUrl) {
          isSelected = true
          this.commit('ADD_SLIDER', f)
        }
      })
      state.sliderBar.filter((f) => {
        if (f && f.url == url.oldUrl) {
          // flag = true;
          f.newName = decodeURI(url.newUrl)
          state.activeMenu = url.oldUrl
        }
      })
      if (!isSelected) {
        if (state.sliderBarTotal) {
          state.sliderBarTotal.filter((f) => {
            if (f && f.url == url.oldUrl) {
              this.commit('ADD_SLIDER', f)
            }
          })
        }

        state.sliderBar.filter((f) => {
          if (f && f.url == url.oldUrl) {
            // flag = true;
            f.newName = decodeURI(url.newUrl)
            state.activeMenu = url.oldUrl
          }
        })
      }
    }
    // 增加逻辑结束
  },
  GOBACK_NAME(state, url) {
    // state.flag = true;
    state.sliderBar.filter((f) => {
      if (f && f.url == url.oldUrl) {
        f.newName = decodeURI(url.newUrl)
      }
    })
  },
  DEL_KEEP_ALIVE: function (state, name) {
    var index = state.keekAlives.indexOf(name)
    if (index > -1) {
      state.keekAlives.splice(index, 1)
    }
  },
  /**
   * @name: 修改state状态
   * @param {state}
   * @param {e}  Array  ['key','value']
   * @Author: 471826078@qq.com
   */
  SET_INFO(state, e) {
    if (e) {
      state[e[0]] = e[1]
    }
  },
  CHANGE_AREA(state, data) {
    state.changeArea = data
  },
  CHANGE_PROPOSAL_DATA: function (state, obj) {
    state.proposalData = obj
  },
  // 关闭tab
  CLOSE_TAB(state, type) {
    //关闭全部
    if (type == 'all') {
      var arr = []
      var x = state.sliderBar[0]
      arr.push(x)
      state.sliderBar = arr
      //设置选中
      state.activeIndex = x.url
      localStorage.setItem('sliderBar', JSON.stringify(state.sliderBar))
    } else {
      //关闭其他
      var arr = []
      var x = state.sliderBar[0]
      arr.push(x)
      state.sliderBar.filter((f, index) => {
        if (f && f.url == state.activeMenu && f.url != x.url) {
          arr.push(f)
        }
      })
      state.sliderBar = arr
    }
  },
}

export default mutations
